<?php

namespace App\Http\Admin\Controllers;

use App\Http\Admin\Services\AdminRoleService;
use App\Http\Controllers\Controller;

class AdminRoleController extends Controller
{
    public function __construct(public \App\Api\Contracts\ApiResponseContract $api, public \App\Api\Contracts\VerifyReqContract $ver, public AdminRoleService $role_service)
    {
    }


    /**
     * 角色列表
     * @return mixed
     */
    public function list(): mixed
    {
        $params = $this->ver->checkParams([], ['offset:int', 'limit:int']);
        return $this->api->success(datas: $this->role_service->list($params));
    }

    /**
     * 添加角色
     * @return mixed
     * @throws \App\Api\Exceptions\BusinessException
     */
    public function add(): mixed
    {
        $params = $this->ver->checkParams(['name'], ['remark:string', 'identity:string', 'permissions:array']);
        return $this->api->success(datas: $this->role_service->createRole($params));
    }

    /**
     * 更新角色
     * @return mixed
     * @throws \App\Api\Exceptions\BusinessException
     * @throws \Throwable
     */
    public function update()
    {
        $params = $this->ver->checkParams(['id:int'], ['name', 'permissions:array', 'remark:string', 'identity:string']);
        return $this->role_service->updateRole($params) ? $this->api->success('更新成功') : $this->api->fail('更新失败');
    }

    /**
     * 获取指定ID的角色详情
     * @return mixed
     */
    public function info(): mixed
    {
        $params = $this->ver->checkParams(['id:int']);
        return $this->api->success(datas: $this->role_service->info($params['id']));
    }

    /**
     * 刪除角色
     * @return mixed
     * @throws \App\Api\Exceptions\BusinessException
     * @throws \Throwable
     */
    public function delete(): mixed
    {
        $params = $this->ver->checkParams(['id:int']);
        $this->role_service->deleteRole($params['id']);
        return $this->api->success('刪除成功');
    }

}
